#include<algorithm>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
char strnumtmp[51];
char strnum[51];
int main()
{
    freopen("test.txt","r",stdin);
	int casenum;
	scanf("%d\n",&casenum);
	while(casenum--)
	{
	    memset(strnum,0,sizeof(strnum));
		gets(strnumtmp);
		int pStrnum=0;
		for(int i=0;i<strlen(strnumtmp);i++)
		{
			if(strnumtmp[i]>='0'&&strnumtmp[i]<='9')
			{
				strnum[pStrnum++]=strnumtmp[i];
			}
		}
		strnum[pStrnum]='\0';
		int minx=1<<28;
		int len=strlen(strnum);
		do
        {
            int k=len/2;
            int sum1=0;
            int sum2=0;
            if((strnum[0]=='0'&&k>1)||(strnum[k]=='0'&&len>=3))
                continue;
            for(int i=0;i<k;i++)
                sum1=sum1*10+strnum[i]-'0';
            for(int i=k;i<len;i++)
                sum2=sum2*10+strnum[i]-'0';
            if(sum2-sum1>0&&sum2-sum1<minx)
            {
                minx=sum2-sum1;
                //cout<<sum2<<endl<<sum1<<endl<<"-----"<<endl;
            }

        }while(next_permutation(strnum,strnum+len));
        printf("%d\n",minx);
	}
    return 0;
}